<template>
  <div class="storage">
    <a-tree
      class="storage_tree"
      v-if="treeData"
      :replaceFields="{
        children: 'children',
        title: general_config.primarykey,
        key: 'id',
      }"
      :tree-data="treeData"
      defaultExpandAll
      :selectedKeys="selectedKeys"
      @select="onSelect"
    >
    </a-tree>
  </div>
</template>
<script>
import { getGeneralValue } from '@/api/apply/apl_form.js'
export default {
  props: {
    // 通用组件核心配置
    general_config: {
      type: Object,
      require: true,
    },
  },
  data() {
    return {
      delList: [], // 逻辑删除的产品项
      faid: null, // 原始主键id
      addRecordList: [], // 新增的记一下id
      visible: false,
      treeData: [],
      selectedKeys: [], // 默认选中的树节点
    }
  },
  async created() {
    await this.getValue()
  },
  methods: {
    async getValue() {
      let body = {
        apltabl: this.general_config.apltable,
        cornumber: this.$store.state.Processform.formData['apl_main#cornumber'] || null,
        enttabl: this.general_config.enttable,
        filter: this.general_config.filter || {},
        isAdd: false,
        maprelation: this.general_config.maprelation || {},
      }
      await getGeneralValue(this.$route.params.applyid, body).then((res) => {
        console.log('通用树列表数据', res)
        if (res.code == 0) {
          this.treeData = res.data || []
          if (this.treeData.length > 0) {
            this.getFirstNode(this.treeData[0])
          }
        }
      })
    },
    // 默认选中树第一个
    getFirstNode(itme) {
      this.selectedKeys = [itme.id]
      this.$emit('f_getSelectData', itme)
    },
    // 树选中
    onSelect(ks, bol) {
      this.selectedKeys = ks
      // ks 层级数组
      this.$emit('f_getSelectData', bol.node.$vnode.data.props.dataRef)
    },
  },
}
</script>

<style lang="less" scoped>
.icon {
  color: rgb(70, 158, 240);
  margin-right: 6px;
}
.btn {
  right: 10px;
}
.storage {
  position: relative;

  &_tree {
    position: absolute;
  }
  &_treeIcon {
    margin: 0 -20px 0 10px;
    background-color: #fff;
    display: inline-block;
    padding-left: 5px;
  }
}
</style>